﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;


namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class UAgentMacList : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.删除, Permission.新增, Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UAgentMacList";
            }
        }

        #region 属性 (查询条件)





        protected string ShowAgentName
        {
            get
            {
                return HasPower(Permission.经销商平台_经销商)? OUserController.MutlCmpStr(base.User.UserId):"" ;

            }
        }
        protected string searchAgentName
        {
            get
            {
                return GetRequestValue<string>("searchAgentName", "");

            }
        }
        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }



        protected string IntoStockDateFrom
        {
            get
            {
                return GetRequestValue<string>("IntoStockDateFrom", "");

            }
        }

        protected string IntoStockDateTo
        {
            get
            {
                string temp = GetRequestValue<string>("IntoStockDateTo", "");
                if (string.IsNullOrEmpty(IntoStockDateFrom))
                    return "";
                if (string.IsNullOrEmpty(temp))
                {
                    temp = Convert.ToDateTime(IntoStockDateFrom).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
                }

                return temp;

            }
        }

        /// <summary>
        /// true表示只显示未交机的,否则显示全部
        /// </summary>
        protected bool IsInto
        {
            get
            {
                return GetRequestValue<bool>("IsInto", false);

            }
        }



        protected override string orderField
        {
            get
            {
                return GetRequestValue<string>("orderfield", "IntoStockDate");
            }
        }

        protected override string orderDirection
        {
            get
            {
                return GetRequestValue<string>("orderDirection", "DESC");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            DataBind();
        }

        protected string GetSqlWhere()
        {
            string wheresql = "";
            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SerialNumber))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SerialNumber);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(ShowAgentName))
            {
              

                sqlcommonFiled = string.Format("AgentName in {0}", ShowAgentName);// QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, AgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(searchAgentName))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("AgentName", QueryOperatorsEnum.包含, searchAgentName);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }

            if (IsInto)
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IsInto", QueryOperatorsEnum.等于, "0");
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (IntoStockDateTo != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IntoStockDate", QueryOperatorsEnum.范围, IntoStockDateFrom + "|" + IntoStockDateTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }


            return wheresql;
        }

        protected DataTable SUMDT = null;
        protected void DataBind()
        {
            DataTableCollection DTC = UAgentMacStockController.GetPagination(GetSqlWhere(), pageNum, numPerPage, SQLSort, ref TotalCount);
            this.rpMenu.DataSource = DTC[0];
            this.rpMenu.DataBind();
            SUMDT = DTC[1];
        }

        #region
       
        protected string GetNoSingNum(object d1, object d2)
        {
            if (d1 == DBNull.Value)
                return "";
            if (d2 == DBNull.Value)
                return "";
            return Convert.ToInt32(d1) == 0 ? "0" : (Convert.ToInt32(d1) - Convert.ToInt32(d2)).ToString();
        }

        protected object GetSumDt(string columnname)
        {
            if (SUMDT.Rows.Count <= 0)
                return 0;

            return SUMDT.Rows[0][columnname] == DBNull.Value ? "0" : SUMDT.Rows[0][columnname];
        }
        #endregion

       
    }
}